<?php

namespace Home\Controller;

use Think\Controller;

class AdminController extends Controller {
    // 后台首页
    public function index() {
        $name = session('name');
        if(empty($name)){
            $url = U('loginAdmin');
            echo "<script>top.location.href='$url';</script>";exit;
        }else{
            $this->display();
        }
    }
    // 轮播图列表
    public function adv() {
        $model = M('adv');
        $adv = $model -> select();
        // var_dump($adv);exit;
        $this->assign('adv',$adv);
        $this->display();
    }
    // 添加轮播
    public function addAdv(){
        if(IS_POST){
            $post = I('post.');
            $model = D('adv');
            $result = $model -> AddAdv($post,$_FILES['image']);
            if($result){
                $this -> success('添加成功!',U('Admin/adv'),3);
            }else{
                $this -> error('添加失败!');
            }

        }else{
            $this->display();
        }
        
    }
    // 编辑轮播
    public function editAdv(){
        $model = D('adv');
        if(IS_POST){
            $post = I('post.');
            $result = $model -> EditAdv($post,$_FILES['image']);
            if($result){
                $this -> success('编辑成功！',U('adv'),3);
            }else{
                $this -> error('编辑失败！');
            }
        }else{
            $id = I('get.id');
            $data = $model -> find($id);
            $this -> assign('data',$data);
            $this -> display();
        }
    }
    // 轮播上架
    public function advUp(){
        $model = M('adv');
        $id = I('get.id');
        $data = $model->find($id);
        if($data['status'] == 1){
            $this -> error('已上架！');
        }else{
            $count = $model-> where('status = 1') -> count();
            // var_dump($count);exit;
            if($count < 3){
                $data['status'] = 1;
                $result = $model -> save($data);
                if($result){
                    $this -> success('上架成功!',U('adv'),3);
                }else{
                    $this -> error('上架失败！');
                }
            }else{
                $this -> error('上架个数不能超过3个！');
            }
        }
    }
    // 轮播下架
    public function advDown(){
        $model = M('adv');
        $id = I('get.id');
        $data = $model -> find($id);
        if($data['status'] == 1){
            $data['status'] = 0;
            $result = $model -> save($data);
            if($result){
                $this -> success('下架成功！',U('adv'),3);
            }else{
                $this -> error('下架失败！');
            }
        }else{
            $this -> error('已下架！');
        }    
    }
    // 删除轮播
    public function advDel(){
        $id = I('get.id');
        $result = M('adv') -> delete($id);
        if($result){
            echo json_encode(1);
        }else{
            echo json_encode(0);
        }
    } 
    // 登录页面
    public function loginAdmin() {
        $this->display();
    }
    // 退出登录
    public function logoutAdmin(){
        // 清除缓存
        session(null);
        $url = U('loginAdmin');
        echo "<script>top.location.href='$url'</script>";exit;
    }

    // 验证码类
    public function verify(){
        //1.实例化验证码类
        $config = array(
            'fontSize'  =>  12,           // 验证码字体大小(px)
            'useCurve'  =>  false,        // 是否画混淆曲线
            'useNoise'  =>  false,        // 是否添加杂点
            'imageH'    =>  36,           // 验证码图片高度
            'imageW'    =>  108,           // 验证码图片宽度
            'length'    =>  4,            // 验证码位数
            'codeSet' => '0123456789'     //正则
        );

        ob_clean();

        $Verify  = new \Think\Verify($config);
        //2.调用entry方法产生验证码：
        $Verify ->entry();
    }

    // 登录后台验证
    public function checkLogin() {

        // $post = I('post.');
        // var_dump($post);die;

        $code = I('post.code');
        $verify = new \Think\Verify();
        $result = $verify->check($code);

        if($result){
            $name = I('post.name');
            $password = I('post.password');
            if ($name == 'admin' && $password == 'admin') {
                session('name',$name);
                $this->success('登录成功', U('index'), 3);
            } else {
                $this->error('账户或密码错误！');
            }
        }else{
            $this->error('验证码错误！');
        }
        
    }

    // 效验验证码
    function check_verify($code, $id = '') {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    
    //厦门概况加修改
    public function Xiamen() {
        $model=D('xiamen');
        if (IS_POST) {
            $post = I('POST.');
            $result = $model->XiamenEdit($post, $_FILES['file1'],$_FILES['file2'],$_FILES['file3']);
            if ($result !== false) {
                $this->success('修改成功', U('Xiamen'), 3);
            } else {
                $this->error('修改失败');
            }
            
        }else{
            $data = $model->find();
            // var_dump($data);die;
            $this ->assign('data',$data);
            $this->display();
        }
    }
    //景点列表
    public function sightsList() {
        $model = M('sights');
        // $data = $model->select();
        // 分页第一步,总条数
        $count = $model -> count();
        // 分页第二步，实例化分页类，传递参数
        $page = new \Think\Page($count,3);
        // 分页第三步，制定按钮
        $page -> rollPage = 3;
        $page -> lastSuffix = false;
        $page -> setConfig('prev','上一页');
        $page -> setConfig('next','下一页');
        $page -> setConfig('first','首页');
        $page -> setConfig('last','末页');
        // 分页第四步，使用show方法生成url
        $show = $page -> show();
        // 分页第五步，使用limit方法展示数据
        $data = $model -> limit($page->firstRow,$page->listRows) -> order('id desc') -> select();

        foreach($data as $key => $vol){
            $data[$key]['sights_text'] = html_entity_decode($data[$key]['sights_text']);
        }

        $this->assign('show',$show);
        $this->assign('data', $data);
        $this->display();
    }
    //景点列表添加
    public function addSights() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('sights');
            $result = $model->SightsAdd($post, $_FILES['img1'],$_FILES['img2']);
            if ($result) {
                $this->success('添加成功', U('sightslist'), 3);
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    //景点列表编辑
    public function editSights() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('sights');
            $result = $model->SightsEdit($post,$_FILES['img1'],$_FILES['img2']);
            // var_dump($result);die;
            if($result) {
                $this->success('修改成功', U('sightslist'), 3);
            } else {
                $this->error('修改失败');
            }
        } else {
            $get = I('get.id');
            $model = M('sights');
            $data = $model->find($get);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //删除景点
    public function sightsdel() {
        $id = I('get.id');
        $model = M('sights');
        $data = $model->delete($id);
        if ($data) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }
    }
    // 景点查看详情
    public function showSights(){
        $id = I('get.id');
        $data = M('sights') -> find($id);
        echo htmlspecialchars_decode($data['sights_text']);
    }

    //推荐列表
    public function tuijianList() {
        $model = M('tuijian');
        $data = $model->select();
        $this->assign('data', $data);
        $this->display();
    }
    //推荐添加
    public function addTuijian() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('tuijian');
            $result = $model->TuijianAdd($post, $_FILES['img1'],$_FILES['img2']);
            if ($result) {
                $this->success('添加成功', U('tuijianList'), 3);
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    //推荐列表编辑
    public function editTuijian() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('tuijian');
            $result = $model->TuijianEdit($post,$_FILES['img1'],$_FILES['img2']);
            if ($result !== false) {
                $this->success('修改成功', U('tuijianList'), 3);
            } else {
                $this->error('修改失败');
            }
        } else {
            $get = I('get.id');
            $model = M('tuijian');
            $data = $model->find($get);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //删除推荐
    public function tuijianDel() {
        $id = I('get.id');
        $model = M('tuijian');
        $data = $model->delete($id);
        if ($data) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }

    }
    // 推荐查看详情
    public function showTuijian(){
        $id = I('get.id');
        $data = M('tuijian') -> find($id);
        echo htmlspecialchars_decode($data['tuijian_text']);
    }


    //新闻列表
    public function newsList() {
        $model = M('news');

        // 分页第一步,总条数
        $count = $model -> count();
        // 分页第二步，实例化分页类，传递参数
        $page = new \Think\Page($count,3);
        // 分页第三步，制定按钮
        $page -> rollPage = 3;
        $page -> lastSuffix = false;
        $page -> setConfig('prev','上一页');
        $page -> setConfig('next','下一页');
        $page -> setConfig('first','首页');
        $page -> setConfig('last','末页');
        // 分页第四步，使用show方法生成url
        $show = $page -> show();
        // 分页第五步，使用limit方法展示数据
        $data = $model -> limit($page->firstRow,$page->listRows) -> order('id desc') -> select();
        // $data = $model->select();
        foreach($data as $key => $vol){
            $data[$key]['text'] = html_entity_decode( $data[$key]['text']);
        }
        // var_dump($data);die;
        $this->assign('show',$show);
        $this->assign('data', $data);
        $this->display();
    }

    //新闻添加
    public function addNews() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('news');
            $result = $model->NewsAdd($post, $_FILES['cover_img']);
            if ($result) {
                $this->success('添加成功', U('newsList'), 3);
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }

    //新闻列表编辑
    public function editNews() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('news');
            // var_dump($post);die;
            // var_dump($_FILES['cover_img']);die;
            $result = $model->NewsEdit($post,$_FILES['cover_img']);
            if ($result !== false) {
                $this->success('修改成功', U('newsList'), 3);
            } else {
                $this->error('修改失败');
            }
        } else {
            $get = I('get.id');
            $model = M('news');
            $data = $model->find($get);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //删除新闻
    public function newsDel() {
        $id = I('get.id');
        $model = M('news');
        $data = $model->delete($id);
        if ($data) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }

    }
    // 新闻查看详情
    public function showContent(){
        $id = I('get.id');
        $data = M('news') -> find($id);
        echo htmlspecialchars_decode('摘要：<br><br>'.$data['abstract'].'<br><br><br>');
        echo htmlspecialchars_decode('正文：'.$data['text']);
    }

    //留言列表
    public function book() {
        $model = M('feedback');

        // 分页第一步,总条数
        $count = $model -> count();
        // 分页第二步，实例化分页类，传递参数
        $page = new \Think\Page($count,5);
        // 分页第三步，制定按钮
        $page -> rollPage = 3;
        $page -> lastSuffix = false;
        $page -> setConfig('prev','上一页');
        $page -> setConfig('next','下一页');
        $page -> setConfig('first','首页');
        $page -> setConfig('last','末页');
        // 分页第四步，使用show方法生成url
        $show = $page -> show();
        // 分页第五步，使用limit方法展示数据
        $data = $model -> limit($page->firstRow,$page->listRows) -> order('id desc') -> select();
        $this->assign('show',$show);
        $this->assign('data', $data);
        $this->display();
    }

    //回复留言
    public function feedbackReply(){
        if (IS_POST) {
            $post = I('post.');
            $model = D('feedback');
            $result = $model->save($post);
            if ($result !== false) {
                $this->success('回复成功', U('book'), 3);
            } else {
                $this->error('回复失败');
            }
        } else {
            $get = I('get.id');
            $model = M('feedback');
            $data = $model->find($get);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //删除留言
    public function feedbackDel() {
        $id = I('get.id');
        $model = M('feedback');
        $data = $model->delete($id);
        if ($data) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }

    }

    //展示轮播列表
    public function lunbolist() {
        $model = M('lunbo');
        $data = $model->select();
        $this->assign('data', $data);
        $this->display();
    }

    //轮播列表 修改
    public function lunboedit() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('lunbo');
            $result = $model->LunboEdit($post, $_FILES['file']);
            if ($result !== false) {
                $this->success('修改成功', U('lunbolist'), 3);
            } else {
                $this->error('修改失败');
            }
        } else {
            $get = I('get.id');
            $model = M('lunbo');
            $data = $model->find($get);
            $this->assign('data', $data);
            $this->display();
        }
    }

    //轮播列表添加
    public function lunboadd() {
        if (IS_POST) {
            $post = I('post.');
            $model = D('lunbo');
            $result = $model->LunboAdd($post, $_FILES['file']);
            if ($result) {
                $this->success('添加成功', U('lunbolist'), 3);
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    //删除轮播数据
    public function del_lunbo() {
        $get = I('get.id');
        $model = M('lunbo');
        $result = $model->delete($get);
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
//轮播上架
    public function lunboup() {
        $model = M('lunbo');
        $get = I('get.id');
        $data = $model->find($get);
        $count = $model->where('status = 1')->count();
        if ($count < 3) {
            $data['status'] = 1;
            $result = $model->save($data);
            if ($result) {
                $this->success('上架成功！');
            } else {
                $this->error('上架失败!');
            }
        } else {
            $this->error('上架个数不能超过3个！');
        }
    }
//轮播下架
    public function lunbodowm() {
        $model = M('lunbo');
        $get = I('get.id');
        $rel = $model->find($get);
        if ($rel['status'] == '1') {
            $rel['status'] = 0;
            $result = $model->save($rel);
            if ($result) {
                $this->success('下架成功');
            } else {
                $this->error('下架失败！');
            }
        }
    }
}
